<?php defined('IN_LK') or die('Access Denied');
lk::lo_app('index_common', 'index', 0);
/**
 * 搜索首页
 */
class index extends index_common
{
	private $content_db = null;
	
	public function __construct()
	{
		parent::__construct();
		$this->content_db = lk::load_model('content');
	}
	
	/**
	 * ajax 自动完成
	 */
	public function kwd()
	{
		$_ = isset($_GET['_']) ? $_GET['_'] : exit('');
		//$r = $this->content_db->get("");
		exit("<li>$_</li><li>找家教</li><li>做家教</li><li>一级教师</li><li>看着能用</li><li>其实这块没做</li>");
	}
	
	/**
	 * 搜索
	 */
	public function search()
	{
		$kwd = isset($_GET['q']) ? $_GET['q'] : '';
		$page= isset($_GET['page']) ? $_GET['page'] : 1;
		$kwd = str_replace(array('-', '%', '|'), array('',''), $kwd);
		$break = array(',', 'or', '+', ' ');
		foreach($break as $word)
		{
			if(strpos($kwd, $word) !== false)
			{
				$kwd = str_replace($word, '|', $kwd);
			}
		}
		$redkwd = array_filter(explode('|', $kwd));
		unset($break, $word);
		$kwd = str_replace("|", "%' OR  '%", $kwd);
		$data = $this->content_db->listinfo("*", "`content` LIKE '%$kwd%' OR `title` LIKE '%$kwd%'", $page, 20);
		$page_nav = $this->content_db->page_nav;
		if(is_array($data) && $data)
		{
			foreach($data as $key => $list)
			{
				$data[$key]['updatetime'] = date('m-d H:i', $list['updatetime']);
				if($list['price'] == '0.00')
				{
					$data[$key]['price'] = '面议';
				}
				if(strpos($list['style'], ','))
				{
					$color = $fw = '';
					list($color, $fw) = explode(',', $list['style']);
					$data[$key]['styles'] = " style='color:" . $color . ";font-weight:" . $fw . "'";
				}
				else if($list['style'] == 'bold')
				{
					$data[$key]['styles'] = ' style="font-weight:bold"';
				}
				else if($list['style'])
				{
					$data[$key]['styles'] = ' style="color:' . $list['style'] . '"';
				}
				else
				{
					$data[$key]['styles'] = '';
				}
				$data[$key]['title'] = self::red($list['title'], $redkwd);
			}
		}
		unset($key, $list);
		ob_start();
		include template('content_list', 'index');
		$res = ob_get_clean();
		include template('search_result');
	}
	
	private static function red($str, $word)
	{
		if(is_array($word))
		{
			$replace = array();
			foreach($word as $k => $w)
			{
				$replace[$k] = "<font color='red'>$w</font>";
			}
		}
		else
		{
			$replace = "<font color='red'>$word</font>";
		}
		return str_replace($word, $replace, $str);
	}
}